package com.emm.yixun.mobile.customview;

import java.util.ArrayList;

import android.graphics.Color;

import com.emm.yixun.mobile.model.GetPaymentNotSignedList;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

/**
 * @author
 */
public class BarCharts {

	private String[] color = { "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C",
			"#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D",
			"#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E",
			"#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD",
			"#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D",
			"#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF",
			"#FF8F9D", "#6BF3AD" };

	public void showBarChart(BarChart barChart, BarData barData, String content) {
		// 数据描述
		barChart.setDescription(content);
		// 如果没有数据的时候，会显示这个，类似ListView的EmptyView
		barChart.setNoDataTextDescription("当前没有数据");
		barChart.setNoDataText(" ");
		// 是否显示表格颜色
		barChart.setDrawGridBackground(false);
		// 设置是否可以触摸
		barChart.setTouchEnabled(true);
		// 是否可以拖拽
		barChart.setDragEnabled(false);
		// 是否可以缩放
		barChart.setScaleEnabled(false);
		// 集双指缩放
		barChart.setPinchZoom(false);
		// 设置背景
		barChart.setBackgroundColor(Color.parseColor("#01000000"));
		// 如果打开，背景矩形将出现在已经画好的绘图区域的后边。
		barChart.setDrawGridBackground(false);
		// 集拉杆阴影
		barChart.setDrawBarShadow(false);
		// 图例
		barChart.getLegend().setEnabled(false);

		// 设置数据
		barChart.setData(barData);

		// 隐藏右边的坐标轴 (就是右边的0 - 100 - 200 - 300 ... 和图表中横线)
		barChart.getAxisRight().setEnabled(false);
		// 隐藏左边的左边轴 (同上)
		// barChart.getAxisLeft().setEnabled(false);

		// 网格背景颜色
		barChart.setGridBackgroundColor(Color.parseColor("#00000000"));
		// 是否显示表格颜色
		barChart.setDrawGridBackground(false);
		// 设置边框颜色
		barChart.setBorderColor(Color.parseColor("#00000000"));
		// 说明颜色
		barChart.setDescriptionColor(Color.parseColor("#00000000"));
		// 拉杆阴影
		barChart.setDrawBarShadow(false);
		// 打开或关闭绘制的图表边框。（环绕图表的线）
		barChart.setDrawBorders(false);

		Legend mLegend = barChart.getLegend(); // 设置比例图标示
		// 设置窗体样式
		mLegend.setForm(LegendForm.CIRCLE);
		// 字体
		mLegend.setFormSize(5f);
		// 字体颜色
		mLegend.setTextColor(Color.parseColor("#00000000"));

		XAxis xAxis = barChart.getXAxis();
		xAxis.setPosition(XAxisPosition.BOTTOM);
		xAxis.setDrawGridLines(false);
		xAxis.setSpaceBetweenLabels(2);

		barChart.animateY(1000); // 立即执行的动画,Y轴
	}

	public BarData getBarData(int size, GetPaymentNotSignedList getpayment, String content) {
		ArrayList<String> xValues = new ArrayList<String>();

		for (int i = 0; i < size; i++) {
			xValues.add(getpayment.getPaymentModeList().get(i).getPaymentModeName());// 设置每个壮图的文字描述
		}
		ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();
		for (int i = 0; i < size; i++) {
			yValues.add(new BarEntry(Float.valueOf(getpayment.getPaymentModeList().get(i).getPaymentModeAmount()).floatValue(), i));
		}
		// y轴的数据集合
		BarDataSet barDataSet = new BarDataSet(yValues, content);
		ArrayList<Integer> colors = new ArrayList<Integer>();
		for (int i = 0; i < size; i++) {
			colors.add(Color.parseColor("#5B9BD5"));
		}
		barDataSet.setColors(colors);
		// 设置栏阴影颜色
		barDataSet.setBarShadowColor(Color.parseColor("#01000000"));
		ArrayList<BarDataSet> barDataSets = new ArrayList<BarDataSet>();
		barDataSets.add(barDataSet);
		barDataSet.setValueTextColor(Color.parseColor("#FF8F9D"));
		// 绘制值
		barDataSet.setDrawValues(true);
		if (size >= 3) {
			barDataSet.setBarSpacePercent(55f);
		} else if (size == 2) {
			barDataSet.setBarSpacePercent(70f);
		} else if (size == 1) {
			barDataSet.setBarSpacePercent(85f);
		}
		BarData barData = new BarData(xValues, barDataSets);
		return barData;
	}
}
